package com.panshi.controller.website;

import com.panshi.api.IBannerApi;
import com.panshi.po.BannerPO;
import com.panshi.po.LinksPO;
import com.panshi.qiniuyun.QiniuUtils;
import com.panshi.qiniuyun.UploadImageImp;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class BannerController {

    @Autowired
    IBannerApi iBannerApi;

    @Resource
   UploadImageImp uploadImageImp;

    @GetMapping("banner/query")
    public String query(){
        return "banner/banner-query";
    }

    //配置信息查询
    @PostMapping("banner-query")
    @ResponseBody
    public PageModel<BannerPO> query(PageModel<BannerPO> pm){

        pm.init();

        PageBeanBannerVo pageBeanBannerVo=new PageBeanBannerVo();

        pageBeanBannerVo.setCurrentPage(pm.getCurrentPage());

        pageBeanBannerVo.setPageSize(pm.getPageSize());

        List<BannerPO> list=iBannerApi.queryBannerPostBody(pageBeanBannerVo).getData();

        Integer str=iBannerApi.queryNumberBannerPostBody().getData();

        pm.setList(list);

        pm.setCount(str);

        return pm;
    }

    //模糊查询
    @PostMapping("bannerDimQuery/{vague}")
    @ResponseBody
    public PageModel<BannerPO> dimQuery(PageModel<BannerPO> pm, @PathVariable String  vague){

        pm.init();

        log.info("模糊查询前端传来的数据============={}",vague);

        QueryDimBannerVO queryDimVO=new QueryDimBannerVO();

        queryDimVO.setName(vague);

        queryDimVO.setCurrentPage(pm.getCurrentPage());

        queryDimVO.setPageSize(pm.getPageSize());

        List<BannerPO> list=iBannerApi.queryDimBannerPostBody(queryDimVO).getData();

        log.info("友情链接模糊查询出来的数据==============={}",list);

        Integer str=iBannerApi.queryDimNumberBannerPostBody(vague).getData();

        log.info("查询出来的条数============{}",str);

        pm.setList(list);

        pm.setCount(str);

        return pm;

    }

    @GetMapping("bannerAdd")
    public String bannerAdd() {
        return "banner/banner-add";
    }

    //banner增加
    @PostMapping("bannerAdd")
    @ResponseBody
    public PageModel bannerAdd(@RequestBody AddResponseVO addResponseVO) {

        log.info("前端传来的数据======{}", addResponseVO);

        PageModel pageModel = new PageModel();

        addResponseVO.setCreatedate(new Date());

        ResponseVO str = iBannerApi.addBannerPostBody(addResponseVO);

        Integer option= (Integer) str.getData();

        log.info("添加的返回来的参数==========={}",str);

        if (option == 1) {

            pageModel.setCode(10000);

            pageModel.setMsg(str.getMsg());

        } else if (option == 0) {

            pageModel.setCode(9999);

            pageModel.setMsg(str.getMsg());

        }
        return pageModel;
    }


    @PostMapping("/upload")
    @ResponseBody
    public Map<String,Object> uploadImg(@RequestParam("file") MultipartFile file) throws IOException {

        log.info("上传图片==============={}",file);

        Map<String,Object> map = new HashMap<>();

        //获取文件的名称
        String fileName = file.getOriginalFilename();

        //使用工具类根据上传文件生成唯一的图片名称
        String imgName = QiniuUtils.getRandomImgName(fileName);

        if(!file.isEmpty()){

            FileInputStream stream = (FileInputStream) file.getInputStream();

            String path = uploadImageImp.uploadQNImg(stream,imgName);

            path = "http://"+path;

            log.info("1111111=============={}","1111111111111111111111");

            map.put("error",0);
            map.put("url",path);
            return map;
        }
        map.put("error",1);
        map.put("message","上传失败");

        return map;
    }


    //删除方法
    @PostMapping("banner-del")
    @ResponseBody
    public Map<String,Object> del(DeleteResponseVO deleteResponseVO){

        log.info("删除传来的数据========{}",deleteResponseVO);

        Map<String,Object> map = new HashMap<>();

        deleteResponseVO.setUpdatedate(new Date());

        Integer state = iBannerApi.deleteBannerPostBody(deleteResponseVO).getData();

        if(state == 0){

            map.put("success",false);

            map.put("message","删除失败");

        }else if(state == 1){

            map.put("success",true);

            map.put("message","删除成功");

        }

        return map;

    }


    //编辑页面
    @GetMapping("banner-edit")
    public String edit(@RequestParam Integer id, Model model){
        log.info("编辑id==================={}",id);

        BannerPO bannerPO=iBannerApi.queryId(id).getData();

        log.info("通过id查询出来的数据============{}",bannerPO);

        model.addAttribute("bannerPO",bannerPO);

        return "banner/banner-edit";
    }


    //编辑修改
    @PostMapping("banner-edit")
    @ResponseBody
    public Map<String,Object> edit(@RequestBody UpdateResponseVO updateResponseVO){

        log.info("修改前端传来的值====={}",updateResponseVO);

        Map<String,Object> map = new HashMap<>();

        updateResponseVO.setUpdatedate(new Date());

        ResponseVO str=iBannerApi.updateBannerPostBody(updateResponseVO);

        Integer option= (Integer) str.getData();

        log.info("option=============={}",option);

        log.info("修改返回来的数据============={}",str);

        if(option==0){

            map.put("success",false);

            map.put("message",str.getMsg());

        }else if(option==1){

            map.put("success",true);

            map.put("message",str.getMsg());
        }

        return  map;

    }

}
